package com.venus.plan.planaction.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.Constants;
import com.venus.common.utils.DateUtil;
import com.venus.common.utils.JsonUtil;
import com.venus.component.page.PageInfo;
import com.venus.model.PlanActionVO;
import com.venus.plan.planaction.form.PlanActionForm;
import com.venus.plan.planaction.service.IPlanActionService;
import com.venus.plan.planresource.service.IPlanResourceService;

public class PlanActionMultiOperateAction extends BaseAction {

	private IPlanActionService planActionService;
	
	private IPlanResourceService planResourceService;
	
	public void setPlanActionService(IPlanActionService planActionService){this.planActionService = planActionService;}

	public void setPlanResourceService(IPlanResourceService planResourceService){this.planResourceService = planResourceService;}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		String strMethod = objPlanActionForm.getMethod();
		log.info("调用方法:"+strMethod);
		if("list".equals(strMethod)){
			return this.list(mapping, objPlanActionForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, objPlanActionForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, objPlanActionForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, objPlanActionForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, objPlanActionForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, objPlanActionForm, request, response);
		}
		return super.execute(mapping, form, request, response);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 只显示当前用户的数据
		lstConditionUtils.add(new ConditionUtil("and", "planResource.pubUser.userId", "=", this.getLoginUser(request).getUserId()+"", false));
		// 按日期倒序
		lstConditionUtils.add(new ConditionUtil("order by", "createDate", "", "desc", false));
		// 得到分页信息
		PageInfo objPageInfo = this.planActionService.getPageInfo(objPlanActionForm.getPageRecord(), objPlanActionForm.getCurPage(), lstConditionUtils);
		request.setAttribute("objPageInfo", objPageInfo);
		request.setAttribute("lstPlanActionVOs", objPageInfo.getLstRecords());
		return mapping.findForward("list");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		return mapping.findForward("create");
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		// 新实例
		PlanActionVO objPlanActionVO = new PlanActionVO();
		objPlanActionVO.setActionType(objPlanActionForm.getActionType());
		objPlanActionVO.setCreateDate(new Date());
		objPlanActionVO.setStartDate(DateUtil.parseDate(objPlanActionForm.getStartDate(), "yyyy-MM-dd"));
		objPlanActionVO.setEndDate(DateUtil.parseDate(objPlanActionForm.getEndDate(), "yyyy-MM-dd"));
		objPlanActionVO.setStatus(Constants.PLAN_STATUS_NEW);
		objPlanActionVO.setPriority(objPlanActionForm.getPriority());
		objPlanActionVO.setCurLevel(0);
		objPlanActionVO.setCurLevelExp(0.0f);
		objPlanActionVO.setTotalExp(objPlanActionForm.getTotalExp());
		objPlanActionVO.setNote(objPlanActionForm.getNote());
		objPlanActionVO.setPlanActionName(objPlanActionForm.getPlanActionName());
		objPlanActionVO.setPlanResource(this.planResourceService.loadPlanResourceVO(objPlanActionForm.getPlanResourceId()));
		// 保存实例
		this.planActionService.savePlanActionVO(objPlanActionVO);
		// 实例ID返回到页面
		//request.setAttribute("newPlanActionId", objPlanActionVO.getPlanActionId());
		// 跳转到list页面
		//return this.list(mapping, objPlanActionForm, request, response);
		// 并且json数据
		StringBuffer sb = new StringBuffer(128);
		// 状态
		sb.append("{");
		sb.append("\"startDate\":\"" + DateUtil.Date2String(objPlanActionVO.getStartDate(), "yyyy年MM月dd日") + "\",");
		sb.append("\"endDate\":\"" + DateUtil.Date2String(objPlanActionVO.getEndDate(), "yyyy年MM月dd日") + "\",");
		sb.append("\"status\":\"" + objPlanActionVO.getStatus() + "\",");
		sb.append("\"priority\":\"" + objPlanActionVO.getPriority() + "\",");
		sb.append("\"curLevel\":" + objPlanActionVO.getCurLevel() + ",");
		sb.append("\"curLevelExp\":" + objPlanActionVO.getCurLevelExp() + ",");
		sb.append("\"totalExp\":" + objPlanActionVO.getTotalExp());
		sb.append("}");
		return this.forwardAjax(sb.toString(), response);
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.saveToken(request);
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		PlanActionVO objPlanActionVO = this.planActionService.loadPlanActionVO(objPlanActionForm.getPlanActionId());
		request.setAttribute("objPlanActionVO", objPlanActionVO);
		return mapping.findForward("edit");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		// 加载PlanActionVO实例
		PlanActionVO objPlanActionVO = new PlanActionVO();
		objPlanActionVO.setActionType(objPlanActionForm.getActionType());
		objPlanActionVO.setCreateDate(new Date());
		objPlanActionVO.setStartDate(DateUtil.parseDate(objPlanActionForm.getStartDate(), "yyyy-MM-dd"));
		objPlanActionVO.setEndDate(DateUtil.parseDate(objPlanActionForm.getEndDate(), "yyyy-MM-dd"));
		objPlanActionVO.setStatus(Constants.PLAN_STATUS_NEW);
		objPlanActionVO.setPriority(objPlanActionForm.getPriority());
		objPlanActionVO.setCurLevel(0);
		objPlanActionVO.setCurLevelExp(0.0f);
		objPlanActionVO.setTotalExp(objPlanActionForm.getTotalExp());
		objPlanActionVO.setNote(objPlanActionForm.getNote());
		objPlanActionVO.setPlanResource(this.planResourceService.loadPlanResourceVO(objPlanActionForm.getPlanResourceId()));
		// 修改PlanActionVO实例
		this.planActionService.saveOrUpdatePlanActionVO(objPlanActionVO);
		// 修改的实例ID返回页面
		request.setAttribute("updatePlanActionId", objPlanActionVO.getPlanActionId());
		// 返回list页面
		return this.list(mapping, objPlanActionForm, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		PlanActionForm objPlanActionForm = (PlanActionForm)form;
		// 删除页面选择的多个PlanAction实例
		this.planActionService.deletePlanActionVO(objPlanActionForm.getPlanActionIds());
		return this.list(mapping, form, request, response);
	}
	
}
